//
//  WZNetWorkStatusView.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/19.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "WZNetWorkStatusView.h"

#import "UIImage+GIF.h"

#define YBKNetWorkStausImageNomal @"YBKNetWorkNomal"    //正常
#define YBKNetWorkStausImageError @"aaa"    //错误
#define YBKNetWorkStausImageNoNet @"aaa"    //无网络

@implementation WZNetWorkStatusView

+(instancetype)ViewWithType:(WZNetWorkStatusType)type msg:(NSString *)msg subMsg:(NSString *)subMsg offsetY:(CGFloat)offsetY failClickBlock:(failBlock)failClickBlock{
    
    WZNetWorkStatusView *netWorkView=[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
    netWorkView.type=type;
    netWorkView.msg=msg;
    netWorkView.subMsg=subMsg;
    netWorkView.offSetY=offsetY;
    netWorkView.offSetYContain.constant=offsetY;
    if (WZNetWorkStatusLoadingError==type) {
        netWorkView.failClikBlock=failClickBlock;
    }
    netWorkView.backgroundColor=[UIColor whiteColor];
    
    return netWorkView;
}

-(void)setType:(WZNetWorkStatusType)type{
    [self.indor stopAnimating];
    switch (type) {
        case WZNetWorkStatusLoadingWithImage:{
            self.showImageView.image=[UIImage sd_animatedGIFNamed:YBKNetWorkStausImageNomal];
            self.indor.hidden=YES;
            break;
        }
        case WZNetworkStatusLoadingWithOutImage:{
            self.showImageView.hidden=YES;
            self.indor.hidden=NO;
            [self.indor startAnimating];
            break;
        }
        case WZNetWorkStatusLoadingError:{
            
            self.showImageView.image=[UIImage sd_animatedGIFNamed:YBKNetWorkStausImageError];
            self.indor.hidden=YES;
            break;
        }
        case WZNetWorkStatusLoadingNoNet:{
            
            self.showImageView.image=[UIImage sd_animatedGIFNamed:YBKNetWorkStausImageNoNet];
            self.indor.hidden=YES;
            break;
        }
            
    }
    
}
#pragma mark 失败的action
- (IBAction)failBtnAction:(id)sender {
    //失败的时候调用
    if (_type==WZNetWorkStatusLoadingError&&self.failClikBlock!=nil) {
        self.failClikBlock();
    }
}

-(void)setMsg:(NSString *)msg{
    _msg=msg;
    _msgLabel.text=msg;
    
}

-(void)setSubMsg:(NSString *)subMsg{
    _subMsg=subMsg;
    _subMsgLabel.text=subMsg;
    
}


-(void)didMoveToSuperview{
    
    [super didMoveToSuperview];
    
    UIView *superView=self.superview;
    
    if(superView==nil) return;
    
    UIView *selfView=self;
    
    self.translatesAutoresizingMaskIntoConstraints=NO;
    
    NSDictionary *views=NSDictionaryOfVariableBindings(selfView);
    
    NSString *vfl_H=@"";
    NSString *vfl_V=@"";
    NSDictionary *metrics=nil;
    
    
    if([superView isKindOfClass:[UIScrollView class]]){
        
        UIScrollView *scrollView=(UIScrollView *)superView;
        
        CGSize contentSize=scrollView.contentSize;
        
        BOOL hasContentSize=(CGSizeEqualToSize(contentSize, CGSizeZero));
        
        CGSize size=CGSizeZero;
        if(hasContentSize){
            size=scrollView.contentSize;
        }else{
            size=scrollView.frame.size;
        }
        
        metrics=@{@"w":@(size.width),@"h":@(size.height)};
        vfl_H=@"H:|-0-[selfView(==w)]-0-|";
        vfl_V=@"V:|-0-[selfView(==h)]-0-|";
        
    }else{
        vfl_H=@"H:|-0-[selfView]-0-|";
        vfl_V=@"V:|-0-[selfView]-0-|";
    }
    
    [superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl_H options:0 metrics:metrics views:views]];
    [superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl_V options:0 metrics:metrics views:views]];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
